package com.itheima.health.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.MenuMapper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Set;

/**
 * @Author: liangWeiQUQ
 * @Description: 动态菜单权限功能实现
 * @DateTime: 2021/3/11 9:20
 **/
@DubboService
@Slf4j
public class MenuServiceImpl  implements MenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private UserServiceImpl userService;

    /**
     * @Author: 传闻中的梁大侠
     * @Date: 9:24 2021/3/11
     * @Parms [username]
     * @ReturnType: java.util.List<com.itheima.health.pojo.Menu>
     * @Description: 菜单动态权限显示-->根据username查询权限
     */

    @Override
    public List<Menu> getMenuList(String username) {
        //根据用户名获取user对象
        User user = userService.findUserByName(username);
        //创建菜单权限集合
        List<Menu> menuList = null;
        Set<Role> roles = user.getRoles();
        for (Role role : roles) {
            menuList = menuMapper.getMenuList(role.getId());
            for (Menu menu : menuList) {
                //获取二级菜单
                List<Menu> children = menuMapper.getMenuLists(role.getId(), menu.getId());
                menu.setChildren(children);
            }
        }
        return menuList;
    }
/**
 * @Author: wty
 * @Date: 15:45 2021/3/12
 * @Parms []
 * @ReturnType: java.util.List<com.itheima.health.pojo.Menu>
 * @Description: 查询所有的菜单
*/
    @Override
    @GetMapping("findAllMenu")
    public List<Menu> findAllMenu() {
        List<Menu> menuList = menuMapper.selectAll();
        return menuList;
    }
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        String queryString = queryPageBean.getQueryString();
        Example example = new Example(Menu.class);
        Example.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(queryString)) {
            criteria.orLike("code","%"+queryString+"%");
            criteria.orLike("name","%"+queryString+"%");

        }
        Page<Menu> page = (Page<Menu>) menuMapper.selectByExample(example);
        List<Menu> menuList = page.getResult();
        long total = page.getTotal();
        return new PageResult(total,menuList);
    }

    @Override
    public Menu findById(Integer id) {
        log.info("[菜单---根据id查询菜单项]data: {}",id);
        return menuMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(Menu menu) {
        log.info("[菜单---新增]data: {}", menu);
        menuMapper.insertSelective(menu);
    }

    @Override
    public void deleteById(Integer id) {
        log.info("[菜单---删除]data: {}", id);
        menuMapper.deleteByPrimaryKey(id);

    }

    @Override
    public void edit(Menu menu) {
        log.info("[菜单---修改]data: {}",menu);
        menuMapper.updateByPrimaryKeySelective(menu);

    }

    @Override

    public List<Menu> findAll() {
        log.info("[菜单---查询所有菜单项]data: {}");
        return menuMapper.selectAll();
    }

    @Override
    public Menu findcheckitemIdsById(Integer id) {
        CheckItem checkItem = new CheckItem();
        log.info("[菜单---根据检查项id查询菜单项]data: {}",checkItem.getId());
        return menuMapper.selectByPrimaryKey(checkItem.getId());
    }
}
